// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import "Applications"

AndroidItem {
    id: _root
    width: 720 * viewScale
    height: 1134 * viewScale

    property string imeSimejiPath: ""
    property string phoneDialBackgroundPath: ""

    property bool isImeOn: appReady ? _application.item.imeViewRequest : false    // IME ON

    property string appPackageName: ""      // アプリのパッケージ名
    property string imePackageName: ""      // IMEのパッケージ名

    property bool appReady: _application.status === Loader.Ready    //アプリのロード状態

    onIsImeOnChanged: {
        if(_ime.status === Loader.Ready){
            _ime.visible = isImeOn;
        }
    }

    Column{
        //アプリの表示
        Loader{
            id: _application
            width: _root.width
            height: _root.height - _ime.height
        }
        //IMEの表示
        Loader{
            id: _ime
            width: _root.width
            height: (status === Loader.Ready && visible) ? item.height : 0
        }
    }

    Component.onCompleted: {
//        changeApp(appPackageName);
        changeIme(imePackageName);
    }

    onAppPackageNameChanged: {
        changeApp(appPackageName);
    }

    onImePackageNameChanged: {
        changeIme(imePackageName);
    }

    function changeApp(name){
        if(appPackageName.length > 0){
            _application.sourceComponent = getPackage(name);
        }
    }
    function changeIme(name){
        if(imePackageName.length > 0){
            _ime.sourceComponent = getPackage(name);
        }
    }

    //パッケージ名でコンポーネントを取得する
    function getPackage(name){
        var ret = undefined;
        if(installApplications === undefined){
        }else{
            for(var i=0; i<installApplications.length; i++){
                if(installApplications[i].name === name){
                    ret = installApplications[i].id;
                }
            }
        }
        return ret;
    }



    //インストール済みアプリ（ここに置くかは悩みどころ）
    property variant installApplications: [
        {"id": _comAdamrockerAndroidInputSimeji, "name": "com.adamrocker.android.input.simeji"}
        , {"id": _comGoogleAndroidGooglequicksearchbox, "name": "com.google.android.googlequicksearchbox"}
        , {"id": _comAndroidPhone, "name": "com.android.phone"}
                            ]

    //simeji
    Component{
        id: _comAdamrockerAndroidInputSimeji
        ComAdamrockerAndroidInputSimeji{
            viewScale: _root.viewScale
            isLock: _root.isLock
            visible: _root.isImeOn
            source: _root.imeSimejiPath
        }
    }

    //Google Search
    Component{
        id: _comGoogleAndroidGooglequicksearchbox
        ComGoogleAndroidGooglequicksearchbox{
            viewScale: _root.viewScale
            isLock: _root.isLock
            imeViewRequest: true
        }
    }

    //Phone
    Component{
        id: _comAndroidPhone
        ComAndroidPhone{
            viewScale: _root.viewScale
            isLock: _root.isLock
            dialBackgroundPath: _root.phoneDialBackgroundPath
        }
    }

}
